package com.springcloud.eshop.product.service.impl;

import com.springcloud.eshop.entity.ProductSpecification;
import com.springcloud.eshop.enums.DataTypeEnum;
import com.springcloud.eshop.enums.EventTypeEnum;
import com.springcloud.eshop.message.DataChangeMessage;
import com.springcloud.eshop.product.mapper.ProductSpecificationMapper;
import com.springcloud.eshop.product.service.ProductSpecificationService;
import com.springcloud.eshop.product.service.SendMQService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ProductSpecification)表服务实现类
 *
 * @author makejava
 * @since 2020-06-29 06:11:14
 */
@Service("productSpecificationService")
public class ProductSpecificationServiceImpl extends SendMQService implements ProductSpecificationService {
    @Resource
    private ProductSpecificationMapper productSpecificationMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProductSpecification queryById(Long id) {
        return this.productSpecificationMapper.queryById(id);
    }

    @Override
    public ProductSpecification queryByProductId(Long productId) {
        return this.productSpecificationMapper.queryByProductId(productId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ProductSpecification> queryAllByLimit(int offset, int limit) {
        return this.productSpecificationMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param productSpecification 实例对象
     * @return 实例对象
     */
    @Override
    public ProductSpecification insert(ProductSpecification productSpecification) {
        this.productSpecificationMapper.insert(productSpecification);
        sendMessage(new DataChangeMessage(EventTypeEnum.ADD.getType(), DataTypeEnum.PRODUCT_SPECIFICATION.getType(), productSpecification.getId(), productSpecification.getProductId()));
        return productSpecification;
    }

    /**
     * 修改数据
     *
     * @param productSpecification 实例对象
     * @return 实例对象
     */
    @Override
    public ProductSpecification update(ProductSpecification productSpecification) {
        this.productSpecificationMapper.update(productSpecification);
        sendMessage(new DataChangeMessage(EventTypeEnum.UPDATE.getType(), DataTypeEnum.PRODUCT_SPECIFICATION.getType(), productSpecification.getId(), productSpecification.getProductId()));
        return this.queryById(productSpecification.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        int result = this.productSpecificationMapper.deleteById(id);
        sendMessage(new DataChangeMessage(EventTypeEnum.DELETE.getType(), DataTypeEnum.PRODUCT_SPECIFICATION.getType(), id));
        return result > 0;
    }
}